package com.ztgf.commodity.api.client;

import com.ztgf.commodity.models.bo.CommodityAdvisoryPriceSaveBO;
import com.ztgf.commodity.models.service.ICommodityAdvisoryPriceService;
import com.ztgf.commodity.models.vo.CommodityAdvisoryPriceVO;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 询价表控制器
 *
 * @author chenyu.wang
 * @Date 2020-05-27 11:06:36
 */

@RestController
@RequestMapping("/api/commodityAdvisoryPrice")
@Api(value = "CommodityAdvisoryPriceApiController", tags = "询价表Api接口")
public class CommodityAdvisoryPriceApiController {

    @DubboReference
    private ICommodityAdvisoryPriceService commodityAdvisoryPriceService;

    @PostMapping(value = "/insert")
    @ApiOperation(value = "发布采购需求", notes = "发布采购需求")
    public Result insert(CommodityAdvisoryPriceSaveBO bo) {
        boolean flag = commodityAdvisoryPriceService.add(bo);
        if (flag) {
            return new Result<>(commodityAdvisoryPriceService.queryInquiryResult(bo.getShopId(), 0));
        }
        return new Result<>(CodeEnum.FAIL_100007);
    }

    @PostMapping(value = "/queryInquiryResult")
    @ApiOperation(value = "查询询价结果列表", notes = "查询询价结果列表")
    public Result queryInquiryResult(Long shopId, Integer sort) {
        if (shopId == null) {
            return new Result(CodeEnum.FAIL_100002);
        }
        return new Result<>(commodityAdvisoryPriceService.queryInquiryResult(shopId, sort));
    }

    @PostMapping(value = "/queryAllFinallyCategory")
    @ApiOperation(value = "查询所有最终级类目", notes = "查询所有最终级类目")
    public Result queryAllFinallyCategory() {
        return new Result<>(commodityAdvisoryPriceService.queryAllFinallyCategory());
    }

    @PostMapping(value = "/queryAllBrand")
    @ApiOperation(value = "查询所有品牌", notes = "查询所有品牌")
    public Result queryAllBrand() {
        return new Result<>(commodityAdvisoryPriceService.queryAllBrand());
    }


    @PostMapping(value = "/getSpecificationCategory")
    @ApiOperation(value = "根据分类id查询规格信息", notes = "根据分类id查询规格信息")
    public Result getSpecificationCategory(Long categoryId, String condition) {
        return new Result<>(commodityAdvisoryPriceService.getSpecificationCategory(categoryId, condition));
    }

    @SneakyThrows
    @PostMapping(value = "/queryByFile")
    @ApiOperation(value = "上传excel询价文件并返回结果", notes = "上传excel询价文件并返回结果")
    public Result queryByFile(@RequestParam("file") MultipartFile file) {
        if (file == null || file.isEmpty()) {
            return new Result<>(CodeEnum.FAIL_FILE_400000);
        }
        byte[] input = file.getBytes();
        List<CommodityAdvisoryPriceVO> list = commodityAdvisoryPriceService.queryByFile(input);
        return new Result<>(list);
    }
}
